package com.mjf.ys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.ys.domain.FsYsUserInfo;
import com.mjf.ys.domain.ProductEntity;
import com.mjf.ys.enums.YSApiEnum;
import com.mjf.ys.mapper.FsYsUserInfoMapper;
import com.mjf.ys.service.FsYsUserInfoService;
import com.mjf.ys.util.YSSendUtil;
import com.mjf.ys.vo.ProductDetailVo;
import com.mjf.ys.vo.ProductVo;
import com.mjf.ys.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/29 23:18
 */
@Service
@Slf4j
public class FsYsUserInfoServiceImpl extends ServiceImpl<FsYsUserInfoMapper, FsYsUserInfo> implements FsYsUserInfoService {

    @Autowired
    private YSSendUtil ysSendUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncUser() {
        String sendUrl = ysSendUtil.getSendUrl(YSApiEnum.YS_API_GET_STAFF_LIST_URL);
        MultiValueMap<String, String> resMap = new LinkedMultiValueMap<>();
        log.info("商品同步，开始 {} ", sendUrl);
        resMap.add("pageSize", "500");
        int i = 1;
        boolean  flag = true;
        List<FsYsUserInfo> all = new ArrayList<>();
        while (flag) {
            log.info(" 第 {} 页 ", i);
            resMap.add("pageNum", i + "");
            try {
                String repsp = ysSendUtil.reqForGetWithParam(sendUrl, resMap);
                // 解析接口
                UserInfoVO vo = JSON.parseObject(repsp, UserInfoVO.class);
                if ("200".equals(vo.getCode())) {
                    UserInfoVO.DataVO dataVO = JSON.parseObject(vo.getData(), UserInfoVO.DataVO.class);
                    if (dataVO != null && dataVO.getData() != null
                        && dataVO.getData().getContent() != null && !dataVO.getData().getContent().isEmpty()) {
                        for (UserInfoVO.ContentVO contentVO : dataVO.getData().getContent()) {
                            FsYsUserInfo fsYsUserInfo = new FsYsUserInfo();
                            BeanUtils.copyProperties(contentVO, fsYsUserInfo);
                            fsYsUserInfo.setUserId(contentVO.getUser_id());
                            fsYsUserInfo.setSsStaffJobId(contentVO.getSs_staff_job_id());
                            fsYsUserInfo.setBdStaffbId(contentVO.getBd_staffb_id());
                            fsYsUserInfo.setBdStaffbEnable(contentVO.getBd_staffb_enable());
                            fsYsUserInfo.setSsStaffJobaEndflag(contentVO.getSs_staff_joba_endflag());
                            fsYsUserInfo.setSsStaffJobbJobgradeid(contentVO.getSs_staff_jobb_jobgradeid());
                            all.add(fsYsUserInfo);
                        }
                    } else {
                        flag = false;
                    }
                }
                i ++;
            } catch (Exception e) {
                log.error("商品同步异常", e);
                flag = false;
            }
        }
        all = all.stream().filter(e -> e.getUserId() != null).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(all)) {
            saveOrUpdateBatch(all);
        }
        log.info("商品同步结束,处理数据：{}", all.size());
    }
}
